package com.chengqj.user.service;

import com.chengqj.user.domain.UserDO;
import com.chengqj.user.repository.UserRepository;
import com.chengqj.order.domain.OrderDO;
import com.chengqj.order.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * Copyright © 2020年 tx-demo. All rights reserved.
 *
 * @author chengqj
 * 用户服务类
 * @date 2020/11/13 17:35
 */
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public void updateAmount(Integer userId) {
        Optional<UserDO> userOptional = userRepository.findById(userId);
        if (userOptional.isPresent()){
            UserDO user = userOptional.get();
            user.setAmount(user.getAmount().subtract(new BigDecimal(10)));
            userRepository.save(user);
            return;
        }
        throw new RuntimeException("找不到用户");
    }
}
